#include <bits/stdc++.h>
#include <iterator>

using namespace std;

class Solution {
public:
	vector<int> loudAndRich(vector<vector<int>>& richer, vector<int>& quiet)
	{
		unordered_map<int, vector<int>> map;
		vector in_degree(size(quiet), 0);

		for (auto&& rich_than : richer) {
			auto [o1, o2] = (int(&)[2])rich_than[0];
			in_degree[o2]++;
			map[o1].emplace_back(o2);
		}

		/* vector<int> res(size(quiet)); */
		/* generate(begin(in_degree), end(in_degree), [idx = 0]() mutable { return idx++; }); */
		vector<int> res;
		generate_n(back_inserter(res), size(quiet), [idx = 0]() mutable { return idx++; });
		for (int i = 0, len = size(quiet); i < len; i++) {
			auto curItr = find(begin(in_degree), end(in_degree), 0);
			(*curItr)--;
			int cur = curItr - begin(in_degree);
			for (int pre : map[cur]) {
				if (quiet[res[cur]] < quiet[res[pre]])
					res[pre] = res[cur];
				in_degree[pre]--;
			}
		}

		return res;
	}

private:
};

int main()
{
	[[maybe_unused]] Solution solution;
	return 0;
}
